{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7947a972",
   "metadata": {},
   "source": [
    "在函数内部调用自身本身，这个函数就是递归函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "230ff7dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fact(n):\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    return n * fact(n - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "de8b8972",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fact(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "42d21440",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fact(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da551eab",
   "metadata": {},
   "source": [
    "递归函数的优点是定义简单，逻辑清晰，但是会有栈溢出的可能。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbf596c8",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b15841d5",
   "metadata": {},
   "source": [
    "使用递归函数逻辑简单清晰，过深的调用会导致溢出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46f51fb1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
